package com.kly.user.controller;

import com.fqgj.common.utils.CollectionUtils;
import com.google.common.collect.Maps;
import com.kly.user.db.dao.UserChatterMappingDao;
import com.kly.user.db.entity.UserChatterMappingEntity;
import com.kly.user.msgClient.MsgAPI;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 该接口在添加消息时主动调用
 */
@RestController
public class NoticeController {

    @Resource
    UserChatterMappingDao userChatterMappingDao;
    @Resource
    MsgAPI msgAPI;

    @RequestMapping("/notice")
    public String addMsg(){

        final List<UserChatterMappingEntity> chatters = queryChatters();
        if (CollectionUtils.isEmpty(chatters)) {
            return "NO_CHATTER_EXISTS";
        }

        final List<String> chatterUserCodes = chatters.parallelStream().map(UserChatterMappingEntity::getChatterUserCode).distinct().collect(Collectors.toList());
        chatterUserCodes.forEach(userCode -> {
            msgAPI.postNoticeRequest(userCode);
        });

        return "SUCCESS";
    }

    private List<UserChatterMappingEntity> queryChatters() {
        Map<String, Object> params = Maps.newHashMap();
        params.put("status", 1);
        return userChatterMappingDao.selectByParams(params);
    }

}
